import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;

public class patientsPage extends MainFrame
{
  JLabel title;
  
  /* To know where you are in the patients page */
  int page = 1;
  
  /* Variables */
  Vector data = new Vector();
  ArrayList <Integer> key = new ArrayList();
  static int picked;
  
  
  /* View Patients */
  JComboBox searchFilter;
  JComboBox filter;
  JTextField searchBox;
  JButton btnSearch;
  JList results;
  JScrollPane resultsPane;
  
  /* Patient Profile */
  JTable table;
  JScrollPane scrollPane;
  JButton btnEdit;
  JButton btnDelete;
  JButton btnAdd;
  String row[][] = {};
  String col[] = {"Date","Time","Service Type","Status"};
  editPatientFrame editFrame;
  
  
  /* 1st page of registration */
  JTextField firstName;
  JTextField lastName;
  JTextField birthday;
  JComboBox gender;
  JComboBox maritalStatus;
  JTextField nationality;
  JTextField contactNumbers;
  
  
  /* 2nd page of Registration */
  JButton btnRegister;
  JComboBox service;
  JTextField date;
  JTextField startTime;
  JTextField endTime;
  
  
   public void Draw(){
       
       switch (page){
           
           /* View Patients */
       case 1:
            //<HTML>This is a line<BR>This is another line</HTML>
            title = new JLabel("View Patients");
            title.setFont(new Font("Cambria", Font.PLAIN, 24));
            title.setForeground(Color.black);
            title.setBounds(10,215,300,50);
            MainFrame.panel.add(title);
            
            title = new JLabel("Search");
            title.setFont(new Font("Cambria", Font.PLAIN, 18));
            title.setForeground(Color.black);
            title.setBounds(30,250,300,50);
            MainFrame.panel.add(title);
            
            title = new JLabel("Search Results");
            title.setFont(new Font("Cambria", Font.PLAIN, 18));
            title.setForeground(Color.black);
            title.setBounds(30,320,300,50);
            MainFrame.panel.add(title);
            
            /* Search filter Combobox */
            searchFilter = new JComboBox();
            searchFilter.addItem("All");
            searchFilter.addItem("Name");
            searchFilter.addItem("Gender");
            searchFilter.addItem("Marital Status");
            searchFilter.addItem("Nationality");
            searchFilter.setBackground(Color.WHITE);
            searchFilter.setBounds(30,290,190,25);
            searchFilter.setSelectedIndex(0);
            searchFilter.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
            searchFilter.addActionListener(new ComboBoxListener());
            MainFrame.panel.add(searchFilter);
            searchFilter.updateUI();
            
            filter = new JComboBox();
            filter.setBackground(Color.WHITE);
            filter.setBounds(230,290,190,25);
            filter.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
            filter.setVisible(false);
            MainFrame.panel.add(filter);
            
            /* Search Box input */
            searchBox = new JTextField(10);
            searchBox.setBounds(230,290,190,25);
            searchBox.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
            searchBox.setEnabled(false);
            MainFrame.panel.add(searchBox);
            
            /* Search Button */
            btnSearch = new JButton(new ImageIcon("searchButton.jpg"));
            btnSearch.setBounds(430,290,70,25);
            btnSearch.addActionListener(new ButtonListener());
            MainFrame.panel.add(btnSearch);
            
            /* Search Results JList */
            results = new JList();
            data.removeAllElements();
            results.setFixedCellWidth(400);
            results.setVisibleRowCount(10);
            results.addMouseListener(new ActionJList(results));
            results.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
            resultsPane = new JScrollPane( results );
            resultsPane.setBounds(30,360,470,200);
            resultsPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            panel.add(resultsPane);
            
             try{
                        database.stmt = database.conn.createStatement();

                        database.rs = database.stmt.executeQuery("SELECT * FROM PATIENT ORDER BY LASTNAME");
                               
                        key.clear();
                        data.removeAllElements();
                        while( database.rs.next() ){
                           key.add(database.rs.getInt("patientId"));
                           data.addElement(database.rs.getString("lastName")+", "+database.rs.getString("firstName"));
                        }
                        
                        results.setListData(data);
                           
                } catch( SQLException se ){}
            
            break;
            
            
            /* Patient Profile*/
       case 2:
       
            title = new JLabel("Personal Information ");
            title.setFont(new Font("Cambria", Font.PLAIN, 18));
            title.setForeground(Color.black);
            title.setBounds(30,250,200,50);
            MainFrame.panel.add(title);
            
            title = new JLabel("First Name:");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(30,280,170,50);
            MainFrame.panel.add(title);
       
            title = new JLabel("Last Name: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(30,305,170,50);
            MainFrame.panel.add(title);
       
            title = new JLabel("Birthday: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(30,330,250,50);
            MainFrame.panel.add(title);
            
            
            title = new JLabel("Gender: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(30,355,170,50);
            MainFrame.panel.add(title);
       
            title = new JLabel("Marital Status: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(350,280,170,50);
            MainFrame.panel.add(title);
       
            title = new JLabel("Nationality: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(350,305,250,50);
            MainFrame.panel.add(title);
            
            title = new JLabel("Contact Number/s: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(350,330,250,50);
            MainFrame.panel.add(title);
            
            title = new JLabel("Appointments ");
            title.setFont(new Font("Cambria", Font.PLAIN, 18));
            title.setForeground(Color.black);
            title.setBounds(30,390,200,50);
            MainFrame.panel.add(title);
            
            /* Appointments Table */
            DefaultTableModel model = new DefaultTableModel(row,col) {
                /* Override Cell modification for users not to modify data inside table */
                @Override
                public boolean isCellEditable(int row, int column) {
                    //all cells false
                    return false;
                }
            };;
            model.getDataVector().removeAllElements();
            table = new JTable(model);
            
             key.clear();
             try{
                        database.stmt = database.conn.createStatement();
                               
                        database.rs = database.stmt.executeQuery( 
                                "SELECT  appointmentID, DATE_FORMAT(appointmentDate, '%M %d %Y') as appointmentDate, startTime, endTime, serviceName, status FROM APPOINTMENT p,PATIENT q, SERVICE r WHERE p.SERVICEID = r.SERVICEID" 
                                + " AND p.PATIENTID = q.PATIENTID AND p.PATIENTID = " + picked + " ORDER BY p.appointmentDate DESC, p.startTime ASC");
                                
                                while( database.rs.next() ){
                                        key.add(database.rs.getInt("appointmentID"));
                                        model.insertRow(table.getRowCount(),new Object[]{
                                        database.rs.getString("appointmentDate").equals(homePage.dateToday) ? "Today" : database.rs.getString("appointmentDate"),
                                        database.rs.getString("startTime") + "-" + database.rs.getString("endTime"),
                                        database.rs.getString("serviceName"),
                                        database.rs.getString("status")
                                    });
                                }
             } catch(Exception e){}
                                
            //model.insertRow(table.getRowCount(),new Object[]{"a","b","c","d"});
            table.setFont(new java.awt.Font("Cambria", Font.PLAIN ,15));
            table.getTableHeader().setFont( new Font( "Cambria" , Font.PLAIN , 15 ));
            table.getTableHeader().setBackground( new Color(0, 120, 255, 70) );
            table.getTableHeader().setEnabled(false);
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.addMouseListener(new TableListener());
            scrollPane = new JScrollPane(table);
            scrollPane.setBounds(30,430,680,100);
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.getViewport().setOpaque(false);
            MainFrame.panel.add(scrollPane);
            
             try{
                        // Get a statement from the connection
                        database.stmt = database.conn.createStatement();
                        // Execute the query
                               
                        database.rs = database.stmt.executeQuery("SELECT patientId, lastName, firstName, DATE_FORMAT(birthDate, '%M %d %Y') as birthDate, gender, contactNum, maritalStatus, nationality FROM PATIENT WHERE PATIENTID = "+ picked );
                               
                        while( database.rs.next() ){
                            
                            /* What to put in the Patient Profile (Personal Information) */
                            title = new JLabel(database.rs.getString("lastName")+", "+database.rs.getString("firstName"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 24));
                            title.setForeground(Color.black);
                            title.setBounds(10,215,290,50);
                            MainFrame.panel.add(title);
                            
                            title = new JLabel(database.rs.getString("firstName"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 14));
                            title.setForeground(Color.black);
                            title.setBounds(140,280,170,50);
                            MainFrame.panel.add(title);
                            
                                  
                            title = new JLabel(database.rs.getString("lastName"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 14));
                            title.setForeground(Color.black);
                            title.setBounds(140,305,170,50);
                            MainFrame.panel.add(title);
       
                            title = new JLabel(database.rs.getString("birthDate"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 14));
                            title.setForeground(Color.black);
                            title.setBounds(140,330,250,50);
                            MainFrame.panel.add(title);
                            
                            title = new JLabel(database.rs.getString("gender"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 14));
                            title.setForeground(Color.black);
                            title.setBounds(140,355,170,50);
                            MainFrame.panel.add(title);
                            
                            title = new JLabel(database.rs.getString("contactNum"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 14));
                            title.setForeground(Color.black);
                            title.setBounds(500,330,250,50);
                            MainFrame.panel.add(title);
            
                            title = new JLabel(database.rs.getString("maritalStatus"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 14));
                            title.setForeground(Color.black);
                            title.setBounds(500,280,170,50);
                            MainFrame.panel.add(title);
       
                            title = new JLabel(database.rs.getString("nationality"));
                            title.setFont(new Font("Cambria", Font.PLAIN, 14));
                            title.setForeground(Color.black);
                            title.setBounds(500,305,250,50);
                            MainFrame.panel.add(title);

                        }
                               
                }
                catch( SQLException se ){}
            
             /* Add Patient Button */
            btnAdd = new JButton(new ImageIcon("addbutton.jpg"));
            btnAdd.setBounds(150,405,68,20);
            btnAdd.addActionListener(new ButtonListener());
            MainFrame.panel.add(btnAdd);
            
            /* Edit Patient Profile Button */
            btnEdit = new JButton(new ImageIcon("editButton.jpg"));
            btnEdit.setBounds(250,550,92,25);
            btnEdit.addActionListener(new ButtonListener());
            MainFrame.panel.add(btnEdit);
            
             /* Delete Patient Profile Button */
            btnDelete = new JButton(new ImageIcon("deleteButton.jpg"));
            btnDelete.setBounds(352,550,92,25);
            btnDelete.addActionListener(new ButtonListener());
            MainFrame.panel.add(btnDelete);
            
            break;
            
            /* Register Patients Page 1 */
       case 3:
            title = new JLabel("Register New Patient");
            title.setFont(new Font("Cambria", Font.PLAIN, 24));
            title.setForeground(Color.black);
            title.setBounds(10,215,300,50);
            MainFrame.panel.add(title);
            
            title = new JLabel("Personal Information ");
            title.setFont(new Font("Cambria", Font.PLAIN, 18));
            title.setForeground(Color.black);
            title.setBounds(20,250,200,50);
            MainFrame.panel.add(title);
            
            title = new JLabel("First Name: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(30,280,170,50);
            MainFrame.panel.add(title);
       
            title = new JLabel("Last Name: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(30,315,170,50);
            MainFrame.panel.add(title);
       
            title = new JLabel("Birthday: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(30,350,250,50);
            MainFrame.panel.add(title);
            
            title = new JLabel("Gender: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(30,385,170,50);
            MainFrame.panel.add(title);
       
            title = new JLabel("Marital Status: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(30,420,170,50);
            MainFrame.panel.add(title);
       
            title = new JLabel("Nationality: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(30,455,250,50);
            MainFrame.panel.add(title);
            
            title = new JLabel("Contact Number/s: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(30,490,250,50);
            MainFrame.panel.add(title);
            
            /* First Name input */
            firstName = new JTextField(10);
            firstName.setBounds(220,295,190,25);
            firstName.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
            MainFrame.panel.add(firstName);
       
            /* Middle Name input */
            lastName = new JTextField(10);
            lastName.setBounds(220,330,190,25);
            lastName.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
            MainFrame.panel.add(lastName);
       
            /* Last Name input */
            birthday = new JTextField("YYYY-MM-DD");
            birthday.setBounds(220,365,190,25);
            birthday.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
            MainFrame.panel.add(birthday);
            
            /* Gender Combobox */
            gender = new JComboBox();
            gender.addItem("Male");
            gender.addItem("Female");
            gender.setBackground(Color.WHITE);
            gender.setBounds(220,400,190,25);
            gender.setSelectedIndex(0);
            gender.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
            MainFrame.panel.add(gender);
            gender.updateUI();
            
            /* Marital Status Combobox */
            maritalStatus = new JComboBox();
            maritalStatus.addItem("Single");
            maritalStatus.addItem("Married");
            maritalStatus.setBackground(Color.WHITE);
            maritalStatus.setBounds(220,435,190,25);
            maritalStatus.setSelectedIndex(0);
            maritalStatus.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
            MainFrame.panel.add(maritalStatus);
            
            /* Nationality input */
            nationality = new JTextField(10);
            nationality.setBounds(220,470,190,25);
            nationality.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
            MainFrame.panel.add(nationality);
            
            /* Contact number/s input */
            contactNumbers = new JTextField(10);
            contactNumbers.setBounds(220,505,190,25);
            contactNumbers.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
            MainFrame.panel.add(contactNumbers);
            
            /* Register Patient Button */
            btnRegister = new JButton(new ImageIcon("regPatient.jpg"));
            btnRegister.setBounds(140,545,145,25);
            btnRegister.addActionListener(new ButtonListener());
            MainFrame.panel.add(btnRegister);

            break;
     }
    
    title = new JLabel("_____________________________________________________________");
    title.setFont(new Font("Cambria", Font.BOLD, 14));
    title.setForeground(Color.black);
    title.setBounds(0,220,400,50);
    MainFrame.panel.add(title);
    
  }
  
  public class ButtonListener implements ActionListener{

     public void actionPerformed(ActionEvent e){
       if(e.getSource() == btnAdd){
           appointmentsPage.picked = picked;
           appointmentsPage.fromPatients = true;
           MainFrame.page = 4;
           MainFrame.panel.removeAll();
           MainFrame.appointmentsPage.page = 3;
           MainFrame.appointmentsPage.Draw();
           MainFrame.header.Draw();
           MainFrame.panel.add(MainFrame.backgroundImage);
           MainFrame.panel.repaint();
       }
       else if(e.getSource() == btnRegister){
            JDialog.setDefaultLookAndFeelDecorated(false);
           int response = JOptionPane.showConfirmDialog(null, "Do you want to proceed with the registration?", 
           "Register New Patient", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
           if (response == JOptionPane.YES_OPTION){   
               try{
                    MainFrame.database.stmt = MainFrame.database.conn.createStatement();
                        
                    int numUpdated = 
                    MainFrame.database.stmt.executeUpdate("INSERT INTO Patient (lastName, firstName, birthDate, gender, contactNum, maritalStatus, nationality)" +
                    " VALUES ('"+lastName.getText()+"','" + firstName.getText() +"','"+ birthday.getText() +"','"+ gender.getSelectedItem()+
                    "','"+contactNumbers.getText()+"','"+maritalStatus.getSelectedItem() + "','" + nationality.getText() +"')" ); 
                    
                    
                    MainFrame.database.stmt = MainFrame.database.conn.createStatement();           
                    MainFrame.database.rs = MainFrame.database.stmt.executeQuery("SELECT * FROM PATIENT ORDER BY PATIENTID DESC LIMIT 1");
                    
                    while (MainFrame.database.rs.next() ){
                        picked = MainFrame.database.rs.getInt("patientId"); }
                    
                        JOptionPane.showMessageDialog(null,"Patient registration has been successfully completed.");
               
                        MainFrame.panel.removeAll();
                        MainFrame.patientsPage.page = 2;
                        MainFrame.patientsPage.Draw();
                        MainFrame.header.Draw();
                        MainFrame.panel.add(MainFrame.backgroundImage);
                        MainFrame.panel.repaint();
                    
               }catch( SQLException se ){}
               

           }
        
       }
       else if(e.getSource() == btnSearch){
            try{
                        database.stmt = database.conn.createStatement();
                        switch(searchFilter.getSelectedIndex()){
                           case 0:
                                database.rs = database.stmt.executeQuery("SELECT * FROM PATIENT ORDER BY LASTNAME");
                                break;
                           case 1:
                                database.rs = database.stmt.executeQuery("SELECT * FROM PATIENT WHERE LASTNAME LIKE '%"+searchBox.getText()+"%' OR FIRSTNAME LIKE '%"+searchBox.getText()+"%' ORDER BY LASTNAME");
                                break;
                           case 2:
                                database.rs = database.stmt.executeQuery("SELECT * FROM PATIENT WHERE GENDER LIKE '"+filter.getSelectedItem()+"' ORDER BY LASTNAME");
                                break;
                           case 3:
                                database.rs = database.stmt.executeQuery("SELECT * FROM PATIENT WHERE MARITALSTATUS LIKE '"+filter.getSelectedItem()+"' ORDER BY LASTNAME");
                                break;
                           case 4:
                                database.rs = database.stmt.executeQuery("SELECT * FROM PATIENT WHERE NATIONALITY LIKE '%"+searchBox.getText()+"%' ORDER BY LASTNAME");
                                break;
                        }
                        key.clear();
                        data.removeAllElements();
                        while( database.rs.next() ){
                           key.add(database.rs.getInt("patientId"));
                           data.addElement(database.rs.getString("lastName")+", "+database.rs.getString("firstName"));
                        }
                        results.setListData(data);
                      } catch( SQLException se ){}
       }
       else if(e.getSource() == btnEdit){
               editFrame = new editPatientFrame(new JFrame(), "Edit Patient Information");
               MainFrame.frame.setEnabled(false);
               editFrame.MainFrame(); 
       }
       else if(e.getSource() == btnDelete){
         JDialog.setDefaultLookAndFeelDecorated(false);
           int response = JOptionPane.showConfirmDialog(null, "Do you want to delete this patient? \n"+
           "All appointments related to this patient will also be deleted. Proceed?", 
           "Remove Patient Profile", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
           if (response == JOptionPane.YES_OPTION){   
               
                 try{
                    database.stmt = database.conn.createStatement();
                     int numUpdated = database.stmt.executeUpdate( "DELETE FROM APPOINTMENT WHERE PATIENTID = " + picked );
                    database.stmt = database.conn.createStatement();
                      numUpdated = database.stmt.executeUpdate( "DELETE FROM PATIENT WHERE PATIENTID = " + picked );
                    
                    
                     
                     JOptionPane.showMessageDialog(null,"Patient has been successfully deleted.");
                     MainFrame.panel.removeAll();
                     MainFrame.patientsPage.page = 1;
                     MainFrame.patientsPage.Draw();
                     MainFrame.header.Draw();
                     MainFrame.panel.add(MainFrame.backgroundImage);
                     MainFrame.panel.repaint();
                }catch (Exception ex){System.out.println("Message "+ex.getMessage());}
                
           }
       }
     }
   }
   
   /* Listener for Appointments Table */
   class TableListener extends MouseAdapter {
   public void mouseClicked(MouseEvent e) {
      if (e.getClickCount() == 2 && e.getSource() == table) {
         JTable target = (JTable)e.getSource();
         
         // To know the exact location of the mouse click
         appointmentsPage.picked = key.get(target.getSelectedRow());
         
         // Go to Appointment details on mouse click
          MainFrame.panel.removeAll();
          MainFrame.page = 4;
          MainFrame.appointmentsPage.page = 2;
          MainFrame.appointmentsPage.Draw();
          MainFrame.header.Draw();
          MainFrame.panel.add(MainFrame.backgroundImage);
          MainFrame.panel.repaint();
         }
   }
   }
   
   public class ComboBoxListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
                if(e.getSource() == searchFilter){
                    int comboboxpic=cb.getSelectedIndex();
                    if(comboboxpic == 0){
                        filter.setVisible(false);
                        searchBox.setEnabled(false);
                        searchBox.setVisible(true);
                    }
                    if(comboboxpic == 1){
                        filter.setVisible(false);
                        searchBox.setEnabled(true);
                        searchBox.setVisible(true);
                    }
                    else if(comboboxpic == 2){
                       searchBox.setVisible(false);
                       filter.setVisible(true);
                       filter.removeAllItems();
                       filter.addItem("Male");
                       filter.addItem("Female");
                       filter.setSelectedIndex(0);
                    }
                    else if(comboboxpic == 3){
                       searchBox.setVisible(false);
                       filter.setVisible(true);
                       filter.removeAllItems();
                       filter.addItem("Single");
                       filter.addItem("Married");
                       filter.setSelectedIndex(0);
                    }
                     else if(comboboxpic == 4){
                        filter.setVisible(false);
                        searchBox.setEnabled(true);
                        searchBox.setVisible(true);
                    }
                    searchBox.setText("");
                }
        }
   }
   
     /* Listener for Search Results */
     class ActionJList extends MouseAdapter{
         protected JList list;
    
            public ActionJList(JList l){
                list = l;
            }
    
            public void mouseClicked(MouseEvent e){
                if(e.getSource() == results){
                    if(e.getClickCount() == 2){
             
                        try{
                            int index = list.locationToIndex(e.getPoint());
                            ListModel dlm = list.getModel();
                            Object item = dlm.getElementAt(index);;
                            list.ensureIndexIsVisible(index);
                            
                            picked = key.get(list.getSelectedIndex());
                            MainFrame.panel.removeAll();
                            MainFrame.patientsPage.page = 2;
                            MainFrame.patientsPage.Draw();
                            MainFrame.header.Draw();
                            MainFrame.panel.add(MainFrame.backgroundImage);
                            MainFrame.panel.repaint();
                        }catch(Exception ex){}
                        
                        
                    }
                }
            }
        }
    
    
}

